﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace DEMI_webapp
{
    public partial class Dodanie_Kapelusza : System.Web.UI.Page
    {
        System.Configuration.ConnectionStringSettings connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"];


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CB_inny_CheckedChanged(object sender, EventArgs e)
        {
            if (CB_inny.Checked == true)
                TB_Inny.Enabled = true;
            else
                TB_Inny.Enabled = false;
        }

        protected void Dodaj(string kolor)
        {
            SqlDataReader reader;
            SqlConnection con;
            con = new SqlConnection(connString.ConnectionString);
            con.Open();

            SqlCommand sprawdzenie = new SqlCommand("SELECT id FROM Kapelusze WHERE nazwa=@Nazwa1 and kolor=@Kolor1", con);
            SqlParameter param4 = new SqlParameter();
            param4.ParameterName = "@Nazwa1";
            param4.SqlDbType = SqlDbType.VarChar;
            param4.Value = TB_Nazwa.Text;
            sprawdzenie.Parameters.Add(param4);

            SqlParameter param5 = new SqlParameter();
            param5.ParameterName = "@Kolor1";
            param5.SqlDbType = SqlDbType.VarChar;
            param5.Value = kolor;
            sprawdzenie.Parameters.Add(param5);


            reader = sprawdzenie.ExecuteReader();
            bool czy_istnieje = reader.HasRows;
            reader.Close();

            if (czy_istnieje == false)
            {
                SqlCommand insert = new SqlCommand("INSERT INTO Kapelusze VALUES (@Nazwa,@Kolor,@Cena,@Material,0,null)", con);

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@Nazwa";
                param.SqlDbType = SqlDbType.VarChar;
                param.Value = TB_Nazwa.Text;
                insert.Parameters.Add(param);

                SqlParameter param1 = new SqlParameter();
                param1.ParameterName = "@Cena";
                param1.SqlDbType = SqlDbType.SmallMoney;
                param1.Value = TB_Cena.Text;
                insert.Parameters.Add(param1);

                SqlParameter param2 = new SqlParameter();
                param2.ParameterName = "@Material";
                param2.SqlDbType = SqlDbType.VarChar;
                param2.Value = TB_Material.Text;
                insert.Parameters.Add(param2);

                SqlParameter param3 = new SqlParameter();
                param3.ParameterName = "@Kolor";
                param3.SqlDbType = SqlDbType.VarChar;
                param3.Value = kolor;
                insert.Parameters.Add(param3);

                insert.ExecuteNonQuery();
                Response.Write("<script>alert('Dodano kapelusz')</script>");
                L_uwaga.Text = "";
            }
            else
                Response.Write("<script>alert('Kapelusz już jest w bazie')</script>");

            con.Close();
        }

		protected void Button_Dodaj_Click (object sender, ImageClickEventArgs e)
        {
            bool czy_kolor = false;

            try
            {
                Convert.ToInt32(TB_Cena.Text);
            }
            catch
            {
                L_uwaga.Text = "Zły format ceny. Nie dodano kapelusza!";
                return;
            }

            if (CB_Bialy.Checked == true)
            {
                Dodaj("biały");
                czy_kolor = true;
            }
            if (CB_Czarny.Checked == true)
            {
                Dodaj("czarny");
                czy_kolor = true;
            }
            if (CB_Czerwony.Checked == true)
            {
                Dodaj("czerwony");
                czy_kolor = true;
            }
            if (CB_Zielony.Checked == true)
            {
                Dodaj("zielony");
                czy_kolor = true;
            }
            if (CB_Niebieski.Checked == true)
            {
                Dodaj("niebieski");
                czy_kolor = true;
            }
            if (CB_inny.Checked == true)
            {
                Dodaj(TB_Inny.Text);
                czy_kolor = true;
            }

            if (czy_kolor == false)
            {
                L_uwaga.Text = "Zaznacz jakiś kolor!";
            }
        }
    }
}